package com.pool.db;

import com.config.Config;
import lombok.extern.slf4j.Slf4j;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;

/**
 * @author linyu.dai
 * @project rpc
 * @description 连接工程
 * @creat 2022/12/1 09:49:58
 */
@Slf4j
public class ConnectionFactory {

    private static String url;

    private static String userName;

    private static String password;

    private static String driverName;

    static {
        url = Config.get("datasource.url");
        userName = Config.get("datasource.username");
        password = Config.get("datasource.password");
        driverName = Config.get("datasource.driverClassName");
    }

    //创建数据库连接
    public static Connection createConnection(){
        /*
         * 注册jdbc驱动
         * */
        Driver driver;
        Connection connection = null;
        try {
            driver = (Driver) Class.forName(driverName).getDeclaredConstructor().newInstance();
            DriverManager.registerDriver(driver);
            connection = DriverManager.getConnection(url, userName, password);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("数据库连接失败{}",e.getMessage());
        }
        return connection;
    }

}
